define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area", ["100%","100%"]);
            $(".btn-edit").data("area", ["100%","100%"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campus_mall_goods/index' + location.search,
                    add_url: 'campus_mall_goods/add',
                    edit_url: 'campus_mall_goods/edit',
                    multi_url: 'campus_mall_goods/multi',
                    table: 'campus_mall_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price_txt', title: __('Price_txt')},
                        {field: 'campusmallgoodscategory.name', title: __('C_id')},
                        {field: 'goods_name', title: __('Type')},
                        {field: 'type', title: __('类型'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'is_integral', title: __('Whether integral deduction'), formatter: Table.api.formatter.toggle},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $('#mallgoodschoose').click(function(){
                var url = '/manystore/campus_goods_choose/index';
                if(!url) return false;
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ['900px','600px'],
                    callback:function(value){
                        console.log(value)
                        //这个value就是传上来的值
                        var index = value .lastIndexOf("+")
                        var type =value .substring(0,index);
                        var value =value .substring(index+1,value.length);
                        var arr = $.parseJSON(value);
                        if(arr.length > 0){
                            var id = arr[0]['id'];
                            //console.log(arr[0]['id']);
                            mall_goods_choose(id,type)
                        }

                        //Layer.alert("接收到回传数据：" + value, {title: "回传数据"});
                    }
                };
                Fast.api.open(url,'选择项目',options);
            })

            function mall_goods_choose(id,type){
                jQuery.post('campus_mall_goods/mall_goods_choose', {
                    id:id,
                    type:type,
                }, function(data) {
                    console.log(data.type)
                    console.log(data)
                    if(data.code == 1){
                        $("#selling_way_3").hide();
                        $("#selling_way").show();
                        $("#selling_way_1").show();
                        $("#goods_name_ajax").html(data.data.type_txt+'-'+data.data.name);
                        $("#type").val(data.data.type);
                        $("#gs_id").val(data.data.id);
                        $("#c-price").val(data.data.price);
                    }else{
                        alert(data.msg);
                    }
                });
            }
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "input[name='row[selling_way]']", function(){
                    console.log($(this).val())
                    var selling_way = $(this).val();
                    if(selling_way == '1'){
                        $("#selling_way_1").show();
                        $("#selling_way_2").hide();
                    }else if(selling_way == '2'){
                        $("#selling_way_1").hide();
                        $("#selling_way_2").show();
                    }
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});